import axios, {
    AxiosRequestConfig,
    AxiosRequestHeaders,
    AxiosResponse,
} from "axios";
import { Toast } from "vant";
import { ContentTypeEnum } from "../types/enums";

import { useNoloStore } from "../store";
import { URL_UP_LOAD_FILE } from "./api";

export const BASE_URL = "https://crbt.nolovr.com/"; //生产环境 只有一套环境

type TAxiosOption = {
    baseURL: string;
    timeout: number;
};

let config = {
    baseURL: BASE_URL,
    timeout: 12000,
};

class Http {
    // service: AxiosInstance;
    service;
    constructor(config: TAxiosOption) {
        this.service = axios.create(config);

        /* 请求拦截  this.service.interceptors.request.use(config => config, error => Promise.reject(error))*/
        this.service.interceptors.request.use(
            async (config: AxiosRequestConfig) => {
                return config;
            },
            (error) => {
                return Promise.reject(error);
            }
        );

        /* 响应拦截   this.service.interceptors.response.use(response => response.data, error => Promise.reject(error))*/
        this.service.interceptors.response.use(
            (response: AxiosResponse<any>) => {
                const { code, data, message, result } = response.data;
                const msg = message || response.data.msg;

                if (code == 0 && result) {
                    return result;
                }
                return response.data || data;
            },
            async (error) => {
                const store = useNoloStore();
                try {
                    const { status, config } = error.response;
                    switch (status) {
                        case 400:
                        case 429:
                            Toast(JSON.stringify(error.response.data));
                            return Promise.reject(error.response.data);
                            break;
                        case 401:
                            Toast("服务器异常，请稍后再试");
                            break;
                        case 500:
                            Toast("服务器异常，请稍后再试");
                            break;
                        default:
                            Toast("服务器异常，请稍后再试");
                            break;
                    }
                    return Promise.reject(error.response.data);
                } catch (e) {
                    return Promise.reject(error);
                }
            }
        );
    }
    get<T>(
        url: string,
        params?: object,
        options = <AxiosRequestConfig>{}
    ): Promise<T> {
        return this.service.get(url, { params, ...options });
    }
    post<T>(
        url: string,
        params?: object,
        options = <AxiosRequestConfig>{}
    ): Promise<T> {
        return this.service.post(url, params, options);
    }
    put<T>(
        url: string,
        params?: object,
        options = <AxiosRequestConfig>{}
    ): Promise<T> {
        return this.service.put(url, params, options);
    }
    delete<T>(
        url: string,
        params?: any,
        options = <AxiosRequestConfig>{}
    ): Promise<T> {
        return this.service.delete(url, { params, ...options });
    }

    setContentType(config: AxiosRequestConfig) {
        if (config.url && config.url === URL_UP_LOAD_FILE) {
            (config.headers as AxiosRequestHeaders)["Content-Type"] =
                ContentTypeEnum.FORM_DATA;
        } else {
            (config.headers as AxiosRequestHeaders)["Content-Type"] =
                ContentTypeEnum.JSON;
        }
    }
}

export default new Http(config);
